package com.ng.desktop.view.dieta;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.ng.framework.view.desktop.NGGUIUtils;

public class CopiarDialog extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = 4201443080751150189L;

	private Integer dia;
	private List<Comida> comidas;
	
	private JTextField textFieldDia;
	private JCheckBox checkBoxDesayuno;
	private JCheckBox checkBoxAlmuerzo;
	private JCheckBox checkBoxCena;
	private JCheckBox checkBoxRefaccion1;
	private JCheckBox checkBoxRefaccion2;
	private JCheckBox checkBoxRefaccion3;
	private JButton buttonAceptar;
	private JButton buttonCancelar;
	
	public CopiarDialog(Frame owner) {
		super(owner, "Copiar", true);
		
		this.buildGui();
		
		this.setSize(new Dimension(400, 300));
		this.setLocationRelativeTo(owner);
		this.setVisible(true);
	}
	
	private void buildGui() {
		Container contentPane = this.getContentPane();
		
		contentPane.setLayout(new GridBagLayout());
		
		contentPane.add(new JLabel("Desde día:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		contentPane.add(this.getTextFieldDia(), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 5, 5), 0, 0));
		
		contentPane.add(this.createPanelComidas(), new GridBagConstraints(0, 1, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
		
		contentPane.add(this.createPanelBotones(), new GridBagConstraints(0, 2, 2, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
	}
	
	private JTextField getTextFieldDia() {
		if(textFieldDia == null) {
			this.textFieldDia = new JTextField(2);
			textFieldDia.setMaximumSize(textFieldDia.getPreferredSize());
		}
		
		return textFieldDia;
	}
	
	private JPanel createPanelComidas() {
		JPanel panelComidas = new JPanel(new GridBagLayout());
		
		panelComidas.add(this.getCheckBoxDesayuno(), new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		panelComidas.add(this.getCheckBoxAlmuerzo(), new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
		panelComidas.add(this.getCheckBoxCena(), new GridBagConstraints(0, 2, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
		panelComidas.add(this.getCheckBoxRefaccion1(), new GridBagConstraints(0, 3, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
		panelComidas.add(this.getCheckBoxRefaccion2(), new GridBagConstraints(0, 4, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
		panelComidas.add(this.getCheckBoxRefaccion3(), new GridBagConstraints(0, 5, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
		
		panelComidas.setBorder(BorderFactory.createTitledBorder("Comidas"));
		
		return panelComidas;
	}
	
	private JPanel createPanelBotones() {
		JPanel panelBotones = new JPanel(new GridBagLayout());
		
		panelBotones.add(this.getButtonAceptar(), new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
		panelBotones.add(this.getButtonCancelar(), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
		
		return panelBotones;
	}
	
	private JCheckBox getCheckBoxDesayuno() {
		if(checkBoxDesayuno == null) {
			this.checkBoxDesayuno = new JCheckBox("Desayuno");
		}
		return checkBoxDesayuno;
	}

	private JCheckBox getCheckBoxAlmuerzo() {
		if(checkBoxAlmuerzo == null) {
			this.checkBoxAlmuerzo = new JCheckBox("Almuerzo");
		}
		return checkBoxAlmuerzo;
	}
	
	private JCheckBox getCheckBoxCena() {
		if(checkBoxCena == null) {
			this.checkBoxCena = new JCheckBox("Cena");
		}
		return checkBoxCena;
	}
	
	private JCheckBox getCheckBoxRefaccion1() {
		if(checkBoxRefaccion1 == null) {
			this.checkBoxRefaccion1 = new JCheckBox("Refacción 1");
		}
		return checkBoxRefaccion1;
	}
	
	private JCheckBox getCheckBoxRefaccion2() {
		if(checkBoxRefaccion2 == null) {
			this.checkBoxRefaccion2 = new JCheckBox("Refacción 2");
		}
		return checkBoxRefaccion2;
	}
	
	private JCheckBox getCheckBoxRefaccion3() {
		if(checkBoxRefaccion3 == null) {
			this.checkBoxRefaccion3 = new JCheckBox("Refacción 3");
		}
		return checkBoxRefaccion3;
	}
	
	private JButton getButtonAceptar() {
		if(buttonAceptar == null) {
			this.buttonAceptar = NGGUIUtils.createTextButton("Aceptar", this); 
		}
		return buttonAceptar;
	}
	
	private JButton getButtonCancelar() {
		if(buttonCancelar == null) {
			this.buttonCancelar = NGGUIUtils.createTextButton("Cancelar", this); 
		}
		return buttonCancelar;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.getButtonAceptar()) {
			if(this.validateInput()) {
				this.dia = Integer.parseInt(this.getTextFieldDia().getText());
				this.comidas = new ArrayList<Comida>();
				if(this.getCheckBoxDesayuno().isSelected()) {
					comidas.add(Comida.DESAYUNO);
				}
				if(this.getCheckBoxAlmuerzo().isSelected())  {
					comidas.add(Comida.ALMUERZO);
				}
				if(this.getCheckBoxCena().isSelected()) {
					comidas.add(Comida.CENA);
				}
				if(this.getCheckBoxRefaccion1().isSelected()) {
					comidas.add(Comida.REFACCION1);
				}
				if(this.getCheckBoxRefaccion2().isSelected()) {
					comidas.add(Comida.REFACCION2);
				}
				if(this.getCheckBoxRefaccion3().isSelected()) {
					comidas.add(Comida.REFACCION3);
				}
				
				this.dispose();
			}
		} else if(e.getSource() == this.getButtonCancelar()) {
			this.dia = null;
			this.comidas = null;
			
			this.dispose();
		}
	}
	
	private boolean validateInput() {
		boolean valid = true;
		
		if(StringUtils.isEmpty(this.getTextFieldDia().getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Desde día' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldDia().requestFocus();
			
			valid = false;
		} else if(!this.validateInteger(this.getTextFieldDia().getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'Desde día' es inválido.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldDia().requestFocus();
			
			valid = false;
		} else if(!this.validateComidas()) {
			JOptionPane.showMessageDialog(this, "Debe seleccionar por lo menos una comida a copiar.", "Error", JOptionPane.ERROR_MESSAGE);
			
			valid = false;
		}
		
		return valid;
	}
	
	private boolean validateInteger(final String s) {
		boolean valid = true;
		
		try {
			Integer.parseInt(s);			
		} catch(NumberFormatException e) {
			valid = false;
		}
		
		return valid;
	}
	
	private boolean validateComidas() {
		return this.getCheckBoxDesayuno().isSelected() ||
		this.getCheckBoxAlmuerzo().isSelected() ||
		this.getCheckBoxCena().isSelected() ||
		this.getCheckBoxRefaccion1().isSelected() ||
		this.getCheckBoxRefaccion2().isSelected() ||
		this.getCheckBoxRefaccion3().isSelected();
	}
	
	public Integer getDia() {
		return dia;
	}
	
	public List<Comida> getComidas() {
		return comidas;
	}
}
